#include "plot2d_dialog.h"
//#include "ui_plot2d_dialog.h"

Plot2D_Dialog::Plot2D_Dialog(Plot_2D_config *Plot2Dconfig,QList<Graph> *Plot2D_list_of_graph,QWidget *parent)
        : Plot2D_DialogBase(parent)
{

    setupUi(this);
    config = Plot2Dconfig;
    list_of_graph = Plot2D_list_of_graph;
    config->update_plot =0;
}

Plot2D_Dialog::~Plot2D_Dialog()
{
    //delete ui_Plot2D_Dialog;
}


void Plot2D_Dialog::on_Grid_Color_clicked()
{

    QString myStyleSheet;
    config->GridColor = QColorDialog::getColor(config->GridColor,this);
    if(!config->GridColor.isValid()) return;
    Grid_Color->setAutoFillBackground(true);
    QTextStream(&myStyleSheet) << "background-color: rgb(" << config->GridColor.toRgb().red()<<","<< config->GridColor.toRgb().green()<<","<<config->GridColor.toRgb().blue()<< ")";
    Grid_Color->setStyleSheet(myStyleSheet);

}

void Plot2D_Dialog::on_BackGround_Color_clicked()
{
    QString myStyleSheet;
    config->BackGroundColor = QColorDialog::getColor(config->BackGroundColor,this);
    if(!config->BackGroundColor.isValid()) return;
    BackGround_Color->setAutoFillBackground(true);
    QTextStream(&myStyleSheet) << "background-color: rgb(" << config->BackGroundColor.toRgb().red()<<","<< config->BackGroundColor.toRgb().green()<<","<<config->BackGroundColor.toRgb().blue()<< ")";
    BackGround_Color->setStyleSheet(myStyleSheet);
}
void Plot2D_Dialog::Load_Default()
{
    QVariant temp;
    qDebug("Load_Default");
    xAxis_label->setText(config->xAxis.Label.toAscii());
    temp = config->xAxis.limit_min;
    Xaxis_start->setText(temp.toString());
    temp = config->xAxis.step;
    Xaxis_step->setText(temp.toString());
    temp = config->xAxis.limit_max;
    Xaxis_stop->setText(temp.toString());
    XAxis_manual->setChecked(config->xAxis.autoScale);
    X_axis_log->setChecked(config->xAxis.log);

    Left_Axis_label->setText(config->ylAxis.Label.toAscii());
    temp = config->ylAxis.limit_min;
    Leftaxis_start->setText(temp.toString());
    temp = config->ylAxis.step;
    Leftaxis_step->setText(temp.toString());
    temp = config->ylAxis.limit_max;
    Leftaxis_stop->setText(temp.toString());
    LeftAxis_manual->setChecked(config->ylAxis.autoScale);
    Left_axis_log->setChecked(config->ylAxis.log);

    Right_Axis_label->setText(config->yrAxis.Label.toAscii());
    temp = config->yrAxis.limit_min;
    Rightaxis_start->setText(temp.toString());
    temp = config->yrAxis.step;
    Rightaxis_step->setText(temp.toString());
    temp = config->yrAxis.limit_max;
    Rightaxis_stop->setText(temp.toString());
    RightAxis_manual->setChecked(config->yrAxis.autoScale);
    Right_axis_log->setChecked(config->yrAxis.log);

    Grid_style->setCurrentIndex(Grid_style->findText(config->gridlinetype));
    Show_Grid->setChecked(config->showgrid);

    QString myStyleSheet;
    myStyleSheet.clear();
    QTextStream(&myStyleSheet) << "background-color: rgb(" << config->GridColor.toRgb().red()<<","<< config->GridColor.toRgb().green()<<","<<config->GridColor.toRgb().blue()<< ")";
    Grid_Color->setStyleSheet(myStyleSheet);

    myStyleSheet.clear();
    QTextStream(&myStyleSheet) << "background-color: rgb(" << config->BackGroundColor.toRgb().red()<<","<< config->BackGroundColor.toRgb().green()<<","<<config->BackGroundColor.toRgb().blue()<< ")";
    BackGround_Color->setStyleSheet(myStyleSheet);


}

void Plot2D_Dialog::Save_Default()
{
    qDebug("Save_Default");
    config->gridlinetype=Grid_style->currentText();
    config->showgrid = Show_Grid->checkState();

    config->xAxis.Label=xAxis_label->text();
    config->xAxis.autoScale=XAxis_manual->checkState();
    config->xAxis.limit_min =Xaxis_start->text().toFloat();
    config->xAxis.limit_max =Xaxis_stop->text().toFloat();
    config->xAxis.step =Xaxis_step->text().toFloat();
    config->xAxis.log= X_axis_log->checkState();

    config->ylAxis.Label=Left_Axis_label->text();
    config->ylAxis.autoScale=LeftAxis_manual->checkState();
    config->ylAxis.limit_min =Leftaxis_start->text().toFloat();
    config->ylAxis.limit_max =Leftaxis_stop->text().toFloat();
    config->ylAxis.step =Leftaxis_step->text().toFloat();
    config->ylAxis.log= Left_axis_log->checkState();

    config->yrAxis.Label=Right_Axis_label->text();
    config->yrAxis.autoScale=RightAxis_manual->checkState();
    config->yrAxis.limit_min =Rightaxis_start->text().toFloat();
    config->yrAxis.limit_max =Rightaxis_stop->text().toFloat();
    config->yrAxis.step =Rightaxis_step->text().toFloat();
    config->yrAxis.log= Right_axis_log->checkState();

}

void Plot2D_Dialog::on_buttonBox_accepted()
{

    config->update_plot =1;
    Save_Default();
    //Plot2D_Dialog::reject();
    close();
}



void Plot2D_Dialog::on_Graph_Color_clicked()
{
    QString myStyleSheet;
    config->Graph_Color = QColorDialog::getColor(config->Graph_Color,this);
    if(!config->Graph_Color.isValid()) return;
    Graph_Color->setAutoFillBackground(true);
    QTextStream(&myStyleSheet) << "background-color: rgb(" << config->Graph_Color.toRgb().red()<<","<< config->Graph_Color.toRgb().green()<<","<<config->Graph_Color.toRgb().blue()<< ")";
    Graph_Color->setStyleSheet(myStyleSheet);
}

Graph::Graph()
{

}

Graph::~Graph()
{
}

void Plot2D_Dialog::on_Graph_add_clicked()
{
    Graph graph;
    QTextStream(&graph.name)  << "test graph:" << qrand();
    //graph.Color=Qt::black;
    graph.Style="Solid Line";
    graph.Color=config->Graph_Color;
    graph.Style=Graph_style->currentText();
    graph.Thick=QVariant(Graph_Tickness->text()).toInt();
    graph.yAxisNo=Graph_axis->currentText();

    graph.Selection_mode = Selection_mode->currentText();
    graph.Datasetname = Dataset_list->currentText();
    graph.Freq = Dataset_freq->currentText();
    graph.Data_onX=Graph_X->currentText();
    graph.Data_onY=Graph_Y->currentText();


    list_of_graph->append(graph);
    update_Graph_List_viewer();
}

void Plot2D_Dialog::update_Graph_List_viewer()
{
    Graph_List->clear();
    for(int i=0;i<list_of_graph->count();i++){
        Graph_List->addItem(list_of_graph->at(i).name);
    }

}

void Plot2D_Dialog::on_Graph_delete_clicked()
{
    int i = Graph_List->currentIndex().row();
    list_of_graph->removeAt(i);
    update_Graph_List_viewer();
}

void Plot2D_Dialog::on_Graph_List_clicked(QModelIndex index)
{
    QString temp,myStyleSheet;
    int i = Graph_List->currentIndex().row();
    QTextStream(&temp) << list_of_graph->at(i).Thick;
    Graph_Tickness->setText(temp);
    Graph_style->setCurrentIndex(Graph_style->findText(list_of_graph->at(i).Style));
    Graph_axis->setCurrentIndex(Graph_axis->findText(list_of_graph->at(i).yAxisNo));

    Graph_Color->setAutoFillBackground(true);
    QTextStream(&myStyleSheet) << "background-color: rgb(" << list_of_graph->at(i).Color.toRgb().red()<<","<< list_of_graph->at(i).Color.toRgb().green()<<","<<list_of_graph->at(i).Color.toRgb().blue()<< ")";
    Graph_Color->setStyleSheet(myStyleSheet);

    Dataset_list->setCurrentIndex(Dataset_list->findText(list_of_graph->at(i).Datasetname));
    Dataset_freq->setCurrentIndex(Dataset_freq->findText(list_of_graph->at(i).Freq));
    Graph_X->setCurrentIndex(Graph_X->findText(list_of_graph->at(i).Data_onX));
    Graph_Y->setCurrentIndex(Graph_Y->findText(list_of_graph->at(i).Data_onY));
    Selection_mode->setCurrentIndex(Selection_mode->findText(list_of_graph->at(i).Selection_mode));



}

void Plot2D_Dialog::on_Graph_apply_clicked()
{
    Graph graph;
    int i = Graph_List->currentIndex().row();
    QTextStream(&graph.name)  << "test graph:" << qrand();
    //graph.Color=Qt::black;
    graph.Style="Solid Line";
    graph.Color=config->Graph_Color;
    graph.Style=Graph_style->currentText();
    graph.Thick=QVariant(Graph_Tickness->text()).toInt();
    graph.yAxisNo=Graph_axis->currentText();

    graph.Selection_mode = Selection_mode->currentText();
    graph.Datasetname = Dataset_list->currentText();
    graph.Freq = Dataset_freq->currentText();
    graph.Data_onX=Graph_X->currentText();
    graph.Data_onY=Graph_Y->currentText();

    list_of_graph->replace(i,graph);
}


void Plot2D_Dialog::on_buttonBox_rejected()
{
    config->update_plot =0;
    //Save_Default();
    //Plot2D_Dialog::reject();
    close();
}

